<template>
  <div class="login_container">
    <div class="content">
      <h1>Hello</h1>
      <h2>欢迎来到硅谷甄选</h2>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="ruleForm.username" :prefix-icon="User" />
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            show-password
            :prefix-icon="Lock"
            @keyup.enter="submitForm(ruleFormRef)"
          />
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" :loading="loading" @click="submitForm(ruleFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage, ElNotification } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { reqLogin } from '@/api/user'
import { useRouter } from 'vue-router'
import { SET_TOKEN } from '@/utils/token'
const router = useRouter()

interface RuleForm {
  username: string
  password: string
}
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  username: '',
  password: '',
})
const rules = reactive<FormRules<RuleForm>>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '长度为3-10', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 10, message: '长度为3-10', trigger: 'blur' },
  ],
})

// 登录按钮loading
const loading = ref(false)

// 登录
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      loading.value = true
      const res = await reqLogin(ruleForm)
      if (res.code === 200) {
        // ElMessage.success('登录成功')
        SET_TOKEN(res.data.token as string)
        router.push('/')
        ElNotification({
          title: '你好',
          message: '欢迎回来',
          type: 'success',
        })
      } else {
        ElMessage.error(res.data.message)
      }
      loading.value = false
    } else {
      ElMessage.error('请按照提示输入用户名或密码')
      return false
    }
  })
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg');
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: right;
  .content {
    width: 500px;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    margin-right: 10%;
    padding: 0 20px 20px;
    color: white;
    .demo-ruleForm {
      margin-top: 20px;
      :deep(.el-form-item__label) {
        color: white;
      }
    }
  }
}
</style>
